package cn.sxt.userscenter.users.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.usercenter.common.util.PageInfoUtil;
import cn.sxt.userscenter.users.dao.IAAdminsDao;
import cn.sxt.userscenter.users.dao.IAUsersDao;
import cn.sxt.userscenter.users.dao.IAUsersExtendDao;
import cn.sxt.userscenter.users.pojo.AAdmins;
import cn.sxt.userscenter.users.pojo.AUsers;
import cn.sxt.userscenter.users.pojo.AUsersExtend;
import cn.sxt.userscenter.users.service.IUsersService;
@Service("usersService")
public class UsersServiceImpl implements IUsersService{

	@Resource
	private IAAdminsDao adminsDao;
	@Resource
	private IAUsersDao usersDao;
	@Resource
	private IAUsersExtendDao usersExtendDao;
	
	@Override
	public JSONObject saveOneAdminsService(AAdmins admins) {
		JSONObject resultJSON = new JSONObject();
		int res = this.adminsDao.save(admins);
		if(res > 0){
			/*存储数据*/
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAdminsService(AAdmins admins) {
		JSONObject resultJSON = new JSONObject();
		int res = this.adminsDao.update(admins);
		if(res > 0){
			/*存储数据*/
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return this.adminsDao.findOne(condMap);
	}

	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil != null){
			/*查询分页*/
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AAdmins> adminsList = this.adminsDao.findCondList(condMap);
			/* 设置总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return adminsList ; 
		}
		/*查询不分页*/
		return this.adminsDao.findCondList(condMap);
	}

	@Override
	public JSONObject saveOneUsersService(AUsers users) {
		JSONObject resultJSON = new JSONObject();
		int res = this.usersDao.save(users);
		if(res > 0){
			/*存储数据*/
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", users.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneUsersService(AUsers users) {
		JSONObject resultJSON = new JSONObject();
		int res = this.usersDao.update(users);
		if(res > 0){
			/*存储数据*/
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", users.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public AUsers findOneUsersService(Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return this.usersDao.findOne(condMap);
	}

	@Override
	public List<AUsers> findCondListUsersService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil != null){
			/*查询分页*/
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AUsers> usersList = this.usersDao.findCondList(condMap);
			/* 设置总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return usersList ; 
		}
		/*查询不分页*/
		return this.usersDao.findCondList(condMap);
	} 
	
	@Override
	public JSONObject saveOneUsersExtendService(AUsersExtend usersExtend) {
		JSONObject resultJSON = new JSONObject();
		int res = this.usersExtendDao.save(usersExtend);
		if(res > 0){
			/*存储数据*/
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", usersExtend.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneUsersExtendService(AUsersExtend usersExtend) {
		JSONObject resultJSON = new JSONObject();
		int res = this.usersExtendDao.update(usersExtend);
		if(res > 0){
			/*存储数据*/
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", usersExtend.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public AUsersExtend findOneUsersExtendService(Map<String, Object> condMap) {
		// TODO Auto-generated method stub
		return this.usersExtendDao.findOne(condMap);
	}

	@Override
	public List<AUsersExtend> findCondListUsersExtendService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil != null){
			/*查询分页*/
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AUsersExtend> usersExtendList = this.usersExtendDao.findCondList(condMap);
			/* 设置总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return usersExtendList ; 
		}
		/*查询不分页*/
		return this.usersExtendDao.findCondList(condMap);
	}

}
